<?php
define('PXF_LOGIN',true);
class ControllerAdmin extends PXFControllerAdmin
{
	static $tablinks = null;

	public function __construct()
	{ 
		parent::__construct();
	}

	public function index()
	{
		PXFConfigAdmin::$site['page_title'] = 'PXFexpress Admin';
		$this->tpl->display('login.index.tpl');
	}

	public function submit()
	{
		/* Validation. */
		$mail = new ValidatorEmail('mail',true);
		$pswd = new ValidatorSimpleText('pswd',true);
		$validator = PXFValidator::getNew();
		$validator->addItem($mail);
		$validator->addItem($pswd);
		$errors = $validator->validate();
		$users = PXFModel::getModel('user');

		/* Login proccess. */
		if(count($errors) == 0)
		{						
			$user = $users->identify($mail->getValue(),$pswd->getValue());
			if($user != null)
			{
				PXFInstance::changeUser($user);
				PXFInstance::pushFlashMessage(PXFConfig::MSG_INFO,'Bienvenido!');
				PXFRequest::redirect(isset($_SESSION['login_success']) ? $_SESSION['login_success'] : 'index.php');
			}
		}		

		PXFConfig::$site['page_title'] = 'Usuario o contrasena invalida';
		$user = $users->getNew();
		$user->mail = $mail->getValue();

		/* ORM Models. */
		$mod_categories = PXFModel::getModel('category');			
		$categories = $mod_categories->findAll();

		/* Template assigns. */
		$this->tpl->assign('site',PXFConfig::$site);
		$this->tpl->assign('categories',$categories);
		$this->tpl->assign('errors',$errors);
		$this->tpl->assign('login_user',$user);
		$this->tpl->display('login.index.tpl');
	}

}
?>
